home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / bbskt30a.zip / MTASK20.ZIP / MTASK.PRT < prev    next >
Text File  |  1990-03-15  |  19KB  |  853 lines

  1.  
  2.  
  3. MTASK 2.0 by Wayne Conrad
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                                 MTASK
  15.  
  16.  
  17. MTASK is a unit for Turbo Pascal 5.5, to allow a Turbo Pascal program
  18. to exhibit simple multi-tasking.  MTASK gives your program a
  19. non-preemptive, request driven multi-tasking capability.  I will
  20. explain what I mean by that later.
  21.  
  22. MTASK 2.0 was written and donated to the public domain by Wayne E.
  23. Conrad (me) in March of 1990.  I may be contact via my BBS,
  24.  
  25.      Pascalaholics Anonymous
  26.      (602) 484-9356
  27.      300/1200/2400 bps
  28.      24 hours/day
  29.  
  30. or by mail at my home:
  31.  
  32.      10 E Bell Road #1001
  33.      Phoenix, AZ  85022
  34.  
  35. I am interested in any modifications, bug reports, or comments you
  36. have.
  37.  
  38. If you modify this package, please keep my name and the name of any
  39. other programmers who've worked on it intact.  Give credit to yourself,
  40. too!  Please distribute the complete package, with documentation and
  41. demonstration programs included.
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                  -1-
  64.  
  65.  
  66.  
  67.  
  68.  
  69. MTASK 2.0 by Wayne Conrad
  70.  
  71.  
  72.  
  73. 1.1 INTRODUCTION
  74.  
  75.  
  76. MTASK allows your Turbo Pascal program to do simple multi-tasking.  I
  77. call MTASK's brand of multi-tasking "non-preemptive, request driven."
  78.  
  79. Preemptive means that the switch from one task to another can happen at
  80. almost any time.  Most preemptive systems have an interrupt driver
  81. hooked to a hardware timer, which causes a task switch every time the
  82. hardware timer goes off.  The advantage of this kind of multi-tasking
  83. is that your programs don't have to be written with multi-tasking in
  84. mind, and don't even have to know that its taking place.  Also, no
  85. program can hog the system for long, because the interrupt driver
  86. switches from one program to another fairly often.  Desqview and
  87. Double-DOS, and OS/2 are operating environments that do preemptive,
  88. interrupt-driven multi-tasking.  The disadvantage of this kind of
  89. multi-tasking is that it can be complex to write and difficult in the
  90. extreme to debug.  These difficulties are compounded in an MS-DOS
  91. environment because MS-DOS was not meant to be used in a multi-tasking
  92. environment.
  93.  
  94. On the other hand, non-preemptive multi-tasking only switches tasks at
  95. certain, well defined times.  There is no interrupt driver that forces
  96. task switches.  In the original Macintosh operating system, for example,
  97. task switches only occured when a task called the operating system.  The
  98. advantage of non-preemptive multi-tasking is that it is much simpler to
  99. write and debug than preemptive multi-tasking, because the system has
  100. total control of when task-switches occur.  The disadvantage to this
  101. form of multi-tasking is that a task must request a task switch often if
  102. the other tasks are to receive their chance to execute.  If a task does
  103. not request a task switch for a long time, the other tasks will appear
  104. to pause.  What's worse, if a task crashes, it won't be able to call the
  105. operating system to let the other tasks execute, so they'll all be hung
  106. too.
  107.  
  108. MTASK implements a very simple method of non-preemptive multi-tasking
  109. that I call "request driven."  Request driven means that task switches
  110. occur only when the current task calls MTASK and requests a switch.
  111. (The sole exception is that a task switch occurs when the current task
  112. terminates itself).  This is about the simplest form of multi-tasking
  113. that I can envision.  It is so simple that the entire MTASK unit
  114. compiles to only about 1400 bytes with stack checking and range
  115. checking turned off, or less if you don't use all of its procedures.
  116. This simplicity also made MTASK easy to write and debug.  MTASK was
  117. written in one day!
  118.  
  119.  
  120.  
  121. 1.2 WHAT ARE MTASK'S LIMITS?
  122.  
  123.  
  124. MTASK allows your program to set up multiple tasks within itself.
  125. These tasks will execute concurrently.  However, it does not effect
  126.  
  127.  
  128.  
  129.                                  -2-
  130.  
  131.  
  132.  
  133.  
  134.  
  135. MTASK 2.0 by Wayne Conrad
  136.  
  137.  
  138.  
  139. anything outside of your program.  It does not allow you to run
  140. multiple programs, multiple copies of COMMAND.COM, or anything else
  141. like that.  It simply allows your program to do several things
  142. concurrently without stumbling over itself.
  143.  
  144. As far as DOS is concerned, your program using MTASK is still just a
  145. simple program.  All of the gymnastics to keep track of multiple tasks
  146. are done by MTASK, withing your program, without the knowledge or
  147. consent of DOS or anything else outside of your program.  Because MTASK
  148. is so simple, it will coexist fine with any "real" multi-tasking DOS
  149. you have set up, such as DesqView or Double-DOS.  Whenever the DOS
  150. gives your program some time, your program will dole out that time to
  151. its tasks.
  152.  
  153. Your program must continue to execute for its tasks to execute.  If any
  154. task in your program exits to DOS for any reason, including a run-time
  155. error, all tasks stop executing.  If one of your tasks shells out by
  156. using Turbo's Exec function, then the other tasks in your program are
  157. suspended until control returns from the Exec function to your program.
  158.  
  159. MTASK must not be made into an overlay.  Any of the tasks it controls
  160. may be overlays, although that may be unwise.  You could end up loading
  161. an overlay from disk during each task switch!
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                  -3-
  196.  
  197.  
  198.  
  199.  
  200.  
  201. MTASK 2.0 by Wayne Conrad
  202.  
  203.  
  204.  
  205. 2.1 SUMMARY OF PROCEDURES AND FUNCTIONS
  206.  
  207.  
  208. To use MTASK, include it in your program's USES statements.  MTASK will
  209. initialize itself automatically, making your main program task #1.
  210. Your program can then use the following procedures and functions to
  211. create and control tasks:
  212.  
  213.  
  214.      create_task         Create another task
  215.  
  216.      terminate_task      Destroy a task
  217.  
  218.      switch_task         Switch to another task
  219.  
  220.      current_task_id     Return the task ID of the current task
  221.  
  222.      number_of_tasks     Return the current number of tasks
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                  -4-
  262.  
  263.  
  264.  
  265.  
  266.  
  267. MTASK 2.0 by Wayne Conrad
  268.  
  269.  
  270.  
  271. 2.1.1 PROCEDURE CREATE_TASK
  272.  
  273.  
  274. PROCEDURE create_task
  275.   (
  276.   task      : task_proc;
  277.   VAR param ;
  278.   stack_size: Word;
  279.   VAR id    : Word;
  280.   VAR result: Word
  281.   );
  282.  
  283.  
  284.      TASK is the procedure to make into a task.  It must match type
  285.      task_proc, having a single variable as its parameter.
  286.  
  287.      PARAM is the parameter to pass to new_task.  It may be a variable
  288.      of any type, so long its what the task expects.  For example, if
  289.      you pass a Word and the task expects a LongInt, the task will get
  290.      invalid data.
  291.  
  292.      STACK_SIZE is the size of the new task's stack.  A stack will be
  293.      allocated from the heap.  The minimum stack size is 512 bytes, but
  294.      most tasks will need more.
  295.  
  296.      ID is set to the task ID of the newly created task.  If the task
  297.      is not created because of an error, then id is not set.
  298.  
  299.      RESULT is the result code, which is set to one of these values:
  300.  
  301.           0                   No error, task created ok
  302.  
  303.           heap_full           Unable to allocate heap for the task's
  304.                               stack
  305.  
  306.           too_many_tasks      Maximum number of tasks are already
  307.                               running
  308.  
  309.  
  310. The new task is created and added to the end of the task list.  The new
  311. task will be executed when the task before it calls switch_task.
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                  -5-
  328.  
  329.  
  330.  
  331.  
  332.  
  333. MTASK 2.0 by Wayne Conrad
  334.  
  335.  
  336.  
  337. 2.1.2 PROCEDURE TERMINATE_TASK
  338.  
  339.  
  340. PROCEDURE terminate_task (id: Word; VAR result: Word);
  341.  
  342.  
  343.      ID is the task id of the task you want to terminate.  If ID = 0,
  344.      then the current task will be terminated.
  345.  
  346.      RESULT is the result code, which is set to one of these values.
  347.  
  348.           0                   No error, task deleted ok
  349.  
  350.           invalid_task_id     There is no task with that ID number
  351.  
  352.  
  353. The designated task will be removed from the task list.  If its stack
  354. was allocated from the heap, it is returned to the heap.
  355.  
  356. If the terminated task is the current task and there is another task